package cn.zucc.edu.handyzucc.controller;

import cn.zucc.edu.handyzucc.entity.*;
import cn.zucc.edu.handyzucc.service.PostService;
import cn.zucc.edu.handyzucc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/post")
public class PostController {
    @Autowired
    private PostService postService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "getAllPost", method = RequestMethod.POST)
    private Map<String, Object> getAllPosts(Integer follow_follower) {
        Map<String, Object> modelMap = new HashMap<>();
        List<Post> allPost = postService.getAllPosts();
        List<User> postAuthor = new ArrayList<>();
        List<Integer> postVotes = new ArrayList<>();
        List<Integer> postFavorites = new ArrayList<>();
        for (Post post : allPost) {
            User author = userService.getAuthorById(post.getPost_author());
            int votetag = postService.getPostVote(post.getPost_id(), follow_follower) ? 0 : 1;
            int favoritetag = postService.getPostFavorite(post.getPost_id(), follow_follower) ? 0 : 1;
            postAuthor.add(author);
            postVotes.add(votetag);
            postFavorites.add(favoritetag);
        }
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", allPost);
        modelMap.put("author", postAuthor);
        modelMap.put("votes", postVotes);
        modelMap.put("favorites", postFavorites);
        return modelMap;
    }

    @RequestMapping(value = "getPostsByAuthor", method = RequestMethod.POST)
    private Map<String, Object> getPostsByAuthor(Integer post_author) {
        Map<String, Object> modelMap = new HashMap<>();
        List<Post> allPost = postService.getPostsByAuthor(post_author);
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", allPost);
        return modelMap;
    }

    @RequestMapping(value = "getFavoritePosts", method = RequestMethod.POST)
    private Map<String, Object> getFavoritePost(Integer postfavorite_userid) {
        Map<String, Object> modelMap = new HashMap<>();
        List<Post> posts = postService.getFavoritePosts(postfavorite_userid);
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", posts);
        return modelMap;
    }

    @RequestMapping(value = "getPostsByFollow", method = RequestMethod.POST)
    private Map<String, Object> getPostsByFollow(Integer follow_follower) {
        Map<String, Object> modelMap = new HashMap<>();
        List<Post> posts = postService.getPostsByFollow(follow_follower);
        List<User> postAuthor = new ArrayList<>();
        List<Integer> postVotes = new ArrayList<>();
        List<Integer> postFavorites = new ArrayList<>();
        for (Post post : posts) {
            User author = userService.getAuthorById(post.getPost_author());
            int votetag = postService.getPostVote(post.getPost_id(), follow_follower) ? 0 : 1;
            int favoritetag = postService.getPostFavorite(post.getPost_id(), follow_follower) ? 0 : 1;
            postAuthor.add(author);
            postVotes.add(votetag);
            postFavorites.add(favoritetag);
        }
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", posts);
        modelMap.put("author", postAuthor);
        modelMap.put("votes", postVotes);
        modelMap.put("favorites", postFavorites);
        return modelMap;
    }

    @RequestMapping(value = "getPostsByKeyword", method = RequestMethod.POST)
    private Map<String, Object> getPostsByKeyword(String keyword, Integer follow_follower) {
        Map<String, Object> modelMap = new HashMap<>();
        List<Post> posts = postService.getPostsByKeyword(keyword);
        List<User> postAuthor = new ArrayList<>();
        List<Integer> postVotes = new ArrayList<>();
        List<Integer> postFavorites = new ArrayList<>();
        for (Post post : posts) {
            User author = userService.getAuthorById(post.getPost_author());
            int votetag = postService.getPostVote(post.getPost_id(), follow_follower) ? 0 : 1;
            int favoritetag = postService.getPostFavorite(post.getPost_id(), follow_follower) ? 0 : 1;
            postAuthor.add(author);
            postVotes.add(votetag);
            postFavorites.add(favoritetag);
        }
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", posts);
        modelMap.put("author", postAuthor);
        modelMap.put("votes", postVotes);
        modelMap.put("favorites", postFavorites);
        return modelMap;
    }

    @RequestMapping(value = "addPost", method = RequestMethod.POST)
    private Map<String, Object> addPost(@RequestBody Post post) {
        Map<String, Object> modelMap = new HashMap<>();
        int postId = postService.addPost(post);
        if (postId != 0) {
            modelMap.put("code", "0");
            modelMap.put("postId", postId);
            modelMap.put("info", "添加成功");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("postId", 0);
            modelMap.put("info", "添加失败");
        }
        return modelMap;
    }

    @RequestMapping(value = "getPostDetail", method = RequestMethod.POST)
    private Map<String, Object> getPostDetail(Integer post_id, Integer post_author) {
        Map<String, Object> modelMap = new HashMap<>();
        Post post = postService.getPostDetail(post_id);
        User author = userService.getAuthorById(post_author);
        modelMap.put("code", "0");
        modelMap.put("post", post);
        modelMap.put("author", author);
        modelMap.put("info", "添加成功");
        return modelMap;
    }

    @RequestMapping(value = "ifAlreadyFollow", method = RequestMethod.POST)
    private Map<String, Object> ifAlreadyFollow(Integer follow_leader, Integer follow_follower) {
        Map<String, Object> modelMap = new HashMap<>();
        if (postService.ifAlreadyFollow(follow_leader, follow_follower)) {
            modelMap.put("code", "0");
            modelMap.put("info", "已关注");
        } else {
            modelMap.put("code", "1");
            modelMap.put("info", "未关注");
        }
        return modelMap;
    }

    @RequestMapping(value = "addFollow", method = RequestMethod.POST)
    private Map<String, Object> addFollow(Integer follow_leader, Integer follow_follower) {
        Map<String, Object> modelMap = new HashMap<>();
        if (postService.addFollow(follow_leader, follow_follower)) {
            modelMap.put("code", "0");
            modelMap.put("info", "关注成功");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("info", "关注失败");
        }
        return modelMap;
    }

    @RequestMapping(value = "cancelFollow", method = RequestMethod.POST)
    private Map<String, Object> cancelFollow(Integer follow_leader, Integer follow_follower) {
        Map<String, Object> modelMap = new HashMap<>();
        if (postService.cancelFollow(follow_leader, follow_follower)) {
            modelMap.put("code", "0");
            modelMap.put("info", "取消关注成功");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("info", "取消关注失败");
        }
        return modelMap;
    }

    @RequestMapping(value = "addPostVote", method = RequestMethod.POST)
    private Map<String, Object> addPostVote(Integer post_id, Integer user_id) {
        Map<String, Object> modelMap = new HashMap<>();
        if (postService.addPostVote(post_id, user_id)) {
            modelMap.put("code", "0");
            modelMap.put("info", "点赞成功");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("info", "点赞失败");
        }
        return modelMap;
    }


    @RequestMapping(value = "cancelPostVote", method = RequestMethod.POST)
    private Map<String, Object> cancelPostVote(Integer post_id, Integer user_id) {
        Map<String, Object> modelMap = new HashMap<>();
        if (postService.cancelPostVote(post_id, user_id)) {
            modelMap.put("code", "0");
            modelMap.put("info", "取消点赞成功");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("info", "取消点赞失败");
        }
        return modelMap;
    }


    @RequestMapping(value = "addPostFavorite", method = RequestMethod.POST)
    private Map<String, Object> addPostFavorite(Integer post_id, Integer user_id) {
        Map<String, Object> modelMap = new HashMap<>();
        if (postService.addPostFavorite(post_id, user_id)) {
            modelMap.put("code", "0");
            modelMap.put("info", "收藏成功");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("info", "收藏失败");
        }
        return modelMap;
    }


    @RequestMapping(value = "cancelPostFavorite", method = RequestMethod.POST)
    private Map<String, Object> cancelPostFavorite(Integer post_id, Integer user_id) {
        Map<String, Object> modelMap = new HashMap<>();
        if (postService.cancelPostFavorite(post_id, user_id)) {
            modelMap.put("code", "0");
            modelMap.put("info", "取消收藏成功");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("info", "取消收藏失败");
        }
        return modelMap;
    }

    @RequestMapping(value = "updatePost", method = RequestMethod.POST)
    private Map<String, Object> updatePost(Integer post_id, String type, Integer step) {
        Map<String, Object> modelMap = new HashMap<>();
        if (postService.updatePost(post_id, type, step)) {
            modelMap.put("code", "0");
            modelMap.put("info", "操作成功");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("info", "操作失败");
        }
        return modelMap;
    }

    @RequestMapping(value = "deletePost", method = RequestMethod.POST)
    private Map<String, Object> deletePost(Integer post_id) {
        Map<String, Object> modelMap = new HashMap<>();
        if (postService.deletePost(post_id)) {
            modelMap.put("code", "0");
            modelMap.put("info", "操作成功");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("info", "操作失败");
        }
        return modelMap;
    }

    @RequestMapping(value = "deleteFavoritePost", method = RequestMethod.POST)
    private Map<String, Object> deleteFavoritePost(Integer postfavorite_postid, Integer postfavorite_userid) {
        Map<String, Object> modelMap = new HashMap<>();
        if (postService.deleteFavoritePost(postfavorite_postid, postfavorite_userid)) {
            modelMap.put("code", "0");
            modelMap.put("info", "操作成功");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("info", "操作失败");
        }
        return modelMap;
    }

    @RequestMapping(value = "getAllPostInVue", method = RequestMethod.GET)
    private Map<String, Object> getAllPostInVue() {
        Map<String, Object> modelMap = new HashMap<>();
        List<Post> posts = postService.getAllPosts();
        modelMap.put("code", "0");
        modelMap.put("data", posts);
        return modelMap;
    }

    @RequestMapping(value = "deletePostInVue", method = RequestMethod.POST)
    private Map<String, Object> deletePostInVue(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        if (postService.deletePostInAdmin(Integer.parseInt(map.get("post_id").toString()))) {
            modelMap.put("code", "0");
            modelMap.put("info", "操作成功");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("info", "操作失败");
        }
        return modelMap;
    }

}
